package office.action.person;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import office.BusiUtil;
import office.action.person.vo.PersonLogVo;
import office.action.person.vo.StatisticsVo;
import office.bo.person.PersonLog;
import office.service.person.IPersonService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import current.security.user.domain.User;
import current.security.user.manager.service.UserManagerService;
import platform.common.action.BaseAction;
import platform.common.bo.Page;
import platform.common.exception.BusinessException;
import platform.util.DateUtil;

@Namespace("/person")
@ParentPackage("struts-base")
@Controller("personAction")
@Scope("prototype")
public class PersonAction extends BaseAction  {
	private static final long serialVersionUID = 1L;
	@Resource
	private IPersonService personalServiceImpl;
	private List<User> users = new ArrayList<User>();
	private UserManagerService userManagerService;
	private PersonLogVo personLogVo;
	private List<PersonLogVo> listLog = new ArrayList<PersonLogVo>();
	private List<String> listContent = new ArrayList<String>();
	private Integer logDay;
	private String flag ;
	private String userId;
	private Page page; // 分页信息
	private Integer year;
	private Integer month;
	private String type = "trip";
	private List<StatisticsVo> statisticsVoList;
	
	@Action(value="gotoLogMenu",results={@Result(location="/pages/person/log/logXml.jsp")})
	public String gotoLogMenu(){
		return "success";
	}
	
	@Action(value="initAddLog",results={@Result(location="/pages/person/log/logAdd.jsp")})
	public String initAddLog(){
		return "success";
	}
	
	@Action(value="getPersonLogById",results={@Result(location="/pages/person/log/logEdit.jsp")})
	public String getPersonLogById(){
		PersonLog personLog = personalServiceImpl.getPersonLogById(personLogVo.getId());
		try {
			BeanUtils.copyProperties(personLogVo, personLog);
		} catch (Exception e) {
			log.error("", e);
		}
		return "success";
	}
	
	@Action(value="gotoDeleteLog",results={@Result(location="/pages/person/log/logDel.jsp")})
	public String gotoDeleteLog(){
		return "success";
	}
	
	@Action(value="updatePersonLog")
	public String updatePersonLog(){
		if(flag != null && !flag.equals("")){
			personalServiceImpl.deletePersonLogById(personLogVo.getId());
		}else{
			PersonLog personLog = personalServiceImpl.getPersonLogById(personLogVo.getId());
			personLog.setContent(personLogVo.getContent());
			personalServiceImpl.savePersonLog(personLog);
		}
		this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="saveOrUpdatePersonLog",results={@Result(type="redirect",location="/person/getPersonLogByUidAndDate.action?logDay=${logDay}")})
	public String saveOrUpdatePersonLog(){
		//日志每天的11点就不能在添加
		if(Integer.valueOf(DateUtil.getHour()).intValue() >= 23){
			this.getAjaxMessagesJson().setMessage(false, "", "编辑失败，今天已经超时，不能添加日志！");
		}else{
			String date = DateUtil.getDate();
			for (Iterator<String> iterator = listContent.iterator(); iterator.hasNext();) {
				String type = (String) iterator.next();
				if(type != null){
					PersonLog personLog = new PersonLog();
					personLog.setContent(type);
					personLog.setUser(personLogVo.getUser());
					personLog.setCreateDate(date);
					personalServiceImpl.savePersonLog(personLog);
				}
			}
			this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
		}
		return RESULT_AJAXJSON;
	}
	
	@Action(value="getPersonLogByUidAndDate",results={@Result(name="one",location="/pages/person/log/log.jsp"),@Result(name="two",location="/pages/person/log/_log.jsp")})
	public String getPersonLogByUidAndDate(){
		if(userId == null || userId.equals("")){
			userId = super.getUserId();
		}
		if(logDay == null){
			logDay = 1;
		}
		if(page == null){
			personLogVo = new PersonLogVo();
			User user = new User();
			user.setId(userId);
			personLogVo.setUser(user);
			personLogVo.setCreateDate(personLogVo.getCreateDate());
			page = personalServiceImpl.getPersonLogTotalPage(personLogVo, logDay);
		}
		getDownUsers();
		List<PersonLog> list = personalServiceImpl.getPersonLogByUidAndDate(page,userId,logDay);
		for (Iterator<PersonLog> iterator = list.iterator(); iterator.hasNext();) {
			PersonLog personLog = (PersonLog) iterator.next();
			PersonLogVo thisPersonLogVo = new PersonLogVo();
			try {
				BeanUtils.copyProperties(thisPersonLogVo, personLog);
				listLog.add(thisPersonLogVo);
			} catch (Exception e) {
				log.error("", e);
			}  
		}
		return  "one";
	}

	@Action(value="getHrSum",results={@Result(name="success",location="/pages/hr/statistics/hrSumList.jsp")})
	public String getHrSum(){
		try {
			if(year == null || year.equals("")){
				year = Integer.valueOf(DateUtil.getCurrentYear());
			}
			if(month == null || month.equals("")){
				month = Integer.valueOf(DateUtil.getCurrentMonth());
			}
			statisticsVoList = personalServiceImpl.getHrSum(year,month,type);
		} catch (BusinessException e) {
			log.error("", e);
		}
		return  "success";
	}
	
	public void getDownUsers(){
		if(BusiUtil.validateRole(getUserRoleName())){
			users = userManagerService.getUsersByAllDepartment(getUserDepartmentId());
		}
	}
	
	public PersonLogVo getPersonLogVo() {
		return personLogVo;
	}

	public void setPersonLogVo(PersonLogVo personLogVo) {
		this.personLogVo = personLogVo;
	}

	public List<PersonLogVo> getListLog() {
		return listLog;
	}

	public void setListLog(List<PersonLogVo> listLog) {
		this.listLog = listLog;
	}

	public IPersonService getPersonalServiceImpl() {
		return personalServiceImpl;
	}

	public void setPersonalServiceImpl(IPersonService personalServiceImpl) {
		this.personalServiceImpl = personalServiceImpl;
	}

	public Integer getLogDay() {
		return logDay;
	}

	public void setLogDay(Integer logDay) {
		this.logDay = logDay;
	}

	public List<String> getListContent() {
		return listContent;
	}

	public void setListContent(List<String> listContent) {
		this.listContent = listContent;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<StatisticsVo> getStatisticsVoList() {
		return statisticsVoList;
	}

	public void setStatisticsVoList(List<StatisticsVo> statisticsVoList) {
		this.statisticsVoList = statisticsVoList;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public Integer getMonth() {
		return month;
	}

	public void setMonth(Integer month) {
		this.month = month;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}
